package com.quanshu.exchange.support.helper;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.exceptions.HyphenateException;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.chat.ChatCommonUtils;
import com.quanshu.exchange.support.chat.ChatConstant;
import com.quanshu.exchange.support.db.UserDao;
import com.quanshu.exchange.support.entity.GroupEntity;
import com.quanshu.exchange.support.entity.UserEntity;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.exchange.ui.gui.HomePageActivity;
import com.quanshu.exchange.ui.gui.message.NotifyMessageListActivity;
import com.quanshu.exchange.ui.gui.message.chat.ChatActivity;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 */
public class NotificationHelper {

    public static final int TYPE_TEXT_MESSAGE = 1;
    public static final int TYPE_EXPRESSION_MESSAGE = 2;
    public static final int TYPE_IMAGE_MWSSAGE = 3;
    public static final int TYPE_VOICE_MESSAGE = 4;
    public static final int TYPE_VIDEO_MESSAGE = 5;
    public static final int TYPE_FILE_MESSAGE = 6;
    //    public static final int TYPE_RED_PACKAGE = 7;
//    public static final int TYPE_NAME_CARD = 8;
//    public static final int TYPE_TRANSFER = 9;
//    public static final int TYPE_GET_MONEY = 10;
    public static final int TYPE_ADD_AS_FRIEND = 11;
    public static final int TYPE_ADD_IN_GROUP = 12;
    public static final int TYPE_RECEIVE_GROUP_APPLY = 13;

    public static int messageFrom = 0;
    private final NotificationManager mNotificationManager;
    private final NotificationCompat.Builder mBuilder;

    private static NotificationHelper instance = null;
    private final Context context;
    private PendingIntent pendingIntent = null;
    private Intent intent = null;

    public synchronized static NotificationHelper getInstance() {
        if (instance == null) {
            instance = new NotificationHelper();
        }
        return instance;
    }


    public NotificationHelper() {
        context = MyApplication.getInstance();
        mNotificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(context);

        mBuilder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle(AppUtils.getAppName(context))
                .setAutoCancel(true)
                .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
                .setTicker(AppUtils.getAppName(context));
    }

    /**
     * 收到加好友信息
     */
    public void showNotification(int notifiType, String title) {
        if (notifiType == TYPE_ADD_AS_FRIEND) {
            messageFrom = 0;
            mBuilder.setContentText(title + ": 请求加你为好友");
            intent = new Intent(context, NotifyMessageListActivity.class);
            pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            mBuilder.setContentIntent(pendingIntent);
            showNotify();
        }
    }

    /**
     * 收到加入群聊
     *
     * @param notifiType
     * @param group
     */
    public void showNotification(int notifiType, GroupEntity group) {
        if (notifiType == TYPE_ADD_IN_GROUP) {
            messageFrom = 1;
            mBuilder.setContentText("你加入了群聊: " + group.getGroup_name());
            intent = null;
            pendingIntent = null;
            intent = new Intent(context, ChatActivity.class);
            intent.putExtra("group", group);
            pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            mBuilder.setContentIntent(pendingIntent);
            showNotify();
        }
    }

    /**
     * 收到加入群聊
     *
     * @param notifiType
     * @param group
     */
    public void showNotification(int notifiType, GroupEntity group, String applicant, String reason) {
        if (notifiType == TYPE_ADD_IN_GROUP) {
            messageFrom = 1;
            mBuilder.setContentText(applicant + "申请加入群聊");
            intent = null;
            pendingIntent = null;
            intent = new Intent(context, NotifyMessageListActivity.class);
            pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            mBuilder.setContentIntent(pendingIntent);
            showNotify();
        }
    }

    /**
     * 收到聊天信息
     */
    public void showNotification(int notifiType, EMMessage message) {
        pendingIntent = null;
        intent = null;
        String userName = null;
        String sourceUserId = null;

        try {
            sourceUserId = message.getStringAttribute("ID");
            UserEntity sourceUser = UserDao.getInstance().query(sourceUserId);
            userName = AppUtils.getUserName(sourceUser);
        } catch (HyphenateException e) {
            e.printStackTrace();
        } finally {
            if (userName == null || "".equals(userName)) {
                userName = message.getUserName();
            }
        }

        if (message.getChatType() == EMMessage.ChatType.Chat) {
            messageFrom = 2;
            switch (notifiType) {
                case TYPE_TEXT_MESSAGE:
                    mBuilder.setContentText(userName + ": " + ChatCommonUtils.getMessageDigest(message, context));
                    break;
                case TYPE_EXPRESSION_MESSAGE:
                    mBuilder.setContentText(userName + ": 发来一个表情");
                    break;
                case TYPE_IMAGE_MWSSAGE:
                    mBuilder.setContentText(userName + ": 发来一张图片");
                    break;
                case TYPE_VOICE_MESSAGE:
                    mBuilder.setContentText(userName + ": 发来一条语音");
                    break;
                case TYPE_VIDEO_MESSAGE:
                    mBuilder.setContentText(userName + ": 发来一段视频");
                    break;
                case TYPE_FILE_MESSAGE:
                    mBuilder.setContentText(userName + ": 发来一个文件");
                    break;
//                case TYPE_RED_PACKAGE:
//                    mBuilder.setContentText(userName + ": 发来红包");
//                    break;
//                case TYPE_NAME_CARD:
//                    String str = null;
//                    EMTextMessageBody content = (EMTextMessageBody) message.getBody();
//                    User user = new Gson().fromJson(content.getMessage(), User.class);
//                    str = AppUtils.getUserTitle(user);
//                    mBuilder.setContentText(userName + ": 发来了'" + str + "'的名片");
//                    break;
//                case TYPE_TRANSFER:
//                    mBuilder.setContentText(userName + ": 向你发起转账");
//                    break;
//                case TYPE_GET_MONEY:
//                    mBuilder.setContentText(userName + ":  向你发起收款");
                default:
                    break;
            }
            // User userFrom = UserDao.queryById(message.getFrom());
            intent = new Intent(context, HomePageActivity.class);
            // intent.putExtra("user",userFrom);
            pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            mBuilder.setContentIntent(pendingIntent);
        }

        if (message.getChatType() == EMMessage.ChatType.GroupChat) {
            messageFrom = 3;
            switch (notifiType) {
                case TYPE_TEXT_MESSAGE:
                    mBuilder.setContentText(userName + ": " + ChatCommonUtils.getMessageDigest(message, context));
                    break;
                case TYPE_EXPRESSION_MESSAGE:
                    mBuilder.setContentText(userName + ": 发来一个表情");
                    break;
                case TYPE_IMAGE_MWSSAGE:
                    mBuilder.setContentText(userName + ": 在群聊里发了一张图片");
                    break;
                case TYPE_VOICE_MESSAGE:
                    mBuilder.setContentText(userName + ": 在群聊里发了一条语音");
                    break;
                case TYPE_VIDEO_MESSAGE:
                    mBuilder.setContentText(userName + ": 在群聊里发了一段视频");
                    break;
                case TYPE_FILE_MESSAGE:
                    mBuilder.setContentText(userName + ": 在群聊里发了一个文件");
                    break;
//                case TYPE_RED_PACKAGE:
//                    mBuilder.setContentText(userName + ": 在群聊里发了红包");
//                    break;
//                case TYPE_NAME_CARD:
//                    String str = null;
//                    try {
//                        if (message.getStringAttribute(ChatFragment.EXT_TYPE_ATTR).equals(ChatFragment.TYPE_EXT_NAME_CARD)) {
//                            EMTextMessageBody content = (EMTextMessageBody) message.getBody();
//                            User user = new Gson().fromJson(content.getMessage(), User.class);
//                            str = AppUtils.getUserTitle(user);
//                            mBuilder.setContentText(userName + ": 在群聊中发了'" + str + "'的名片");
//                        }
//                    } catch (HyphenateException e) {
//                        e.printStackTrace();
//                    } finally {
//                        if (str == null || "".equals(str)) {
//                            mBuilder.setContentText(userName + ":在群聊中发了一个名片");
//                        }
//                    }
//                    break;
//                    case TYPE_TRANSFER:
//                        break;
                default:
                    break;
            }
            intent = new Intent(context, HomePageActivity.class);
            pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            mBuilder.setContentIntent(pendingIntent);
        }

        showNotify();
    }

    public void showNotify() {
        mNotificationManager.notify(messageFrom, mBuilder.build());
    }

    public static int getMessageNotifyType(EMMessage emMessage) {
        int notifiType = NotificationHelper.TYPE_TEXT_MESSAGE;
        String extTypeAttr = "";
        try {
            extTypeAttr = emMessage.getStringAttribute(ChatConstant.Chat_ext_attr_call_type);
        } catch (HyphenateException e) {
            e.printStackTrace();
        }
        if (emMessage.getType() == EMMessage.Type.TXT) {
            if (extTypeAttr == null) {
                notifiType = NotificationHelper.TYPE_TEXT_MESSAGE;
            }
        } else if (emMessage.getType() == EMMessage.Type.IMAGE) {
            notifiType = NotificationHelper.TYPE_IMAGE_MWSSAGE;
        } else if (emMessage.getType() == EMMessage.Type.VOICE) {
            notifiType = NotificationHelper.TYPE_VOICE_MESSAGE;
        } else if (emMessage.getType() == EMMessage.Type.VIDEO) {
            notifiType = NotificationHelper.TYPE_VIDEO_MESSAGE;
        } else if (emMessage.getType() == EMMessage.Type.FILE) {
            notifiType = NotificationHelper.TYPE_FILE_MESSAGE;
        } else {
            notifiType = NotificationHelper.TYPE_TEXT_MESSAGE;
        }
        return notifiType;
    }

}



